package activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.coolweather.lechat.R;
import com.tencent.imsdk.v2.V2TIMAdvancedMsgListener;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMMessage;
import com.tencent.imsdk.v2.V2TIMMessageReceipt;
import com.tencent.imsdk.v2.V2TIMTextElem;
import com.tencent.imsdk.v2.V2TIMValueCallback;

import java.util.ArrayList;
import java.util.List;

import Adapter.MsgAdapter;

public class SendNews extends AppCompatActivity {
    private String txt;
    private ListView msgListView;
    private EditText inputText;
    private Button send;
    private String TAG = "SendNews";
    private MsgAdapter adapter;
    private List<Msg> msgList = new ArrayList<Msg>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        initMsgs();
        setContentView(R.layout.activity_sendnews);
        adapter =  new MsgAdapter(this,R.layout.message_item,msgList);
        inputText = (EditText)findViewById(R.id.input_text);
        send = (Button)findViewById(R.id.send);
        msgListView = (ListView)findViewById(R.id.list_view);
        msgListView.setAdapter(adapter);
        Intent intent = getIntent();
 //       String userId = intent.getStringExtra("userId");
        String userId = "张三";
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                txt =inputText.getText().toString();
                V2TIMManager.getInstance().sendC2CTextMessage(txt, userId, new V2TIMValueCallback<V2TIMMessage>() {
                    @Override
                    public void onError(int i, String s) {
                        Log.e(TAG, "onError: 222222222222222222222");
                    }

                    @Override
                    public void onSuccess(V2TIMMessage v2TIMMessage) {
                        Log.e(TAG, "onSuccess: 111111111111111111111");
                        String content = inputText.getText().toString();
                        if(!"".equals(content)){
                            Msg msg = new Msg(content,Msg.TYPE_SEND);
                            msgList.add(msg);
                            adapter.notifyDataSetChanged();
                            msgListView.setSelection(msgList.size());
                            inputText.setText("");

                        }
                    }
                });
            }
        });
        V2TIMManager.getMessageManager().addAdvancedMsgListener(new V2TIMAdvancedMsgListener() {
            @Override
            public void onRecvNewMessage(V2TIMMessage msg) {
                super.onRecvNewMessage(msg);
                Log.e(TAG, "onRecvNewMessage:收到消息"+msg);
                String text = msg.getTextElem().getText();
                Log.e(TAG, "onRecvNewMessage:++++++++++++"+text);
                Msg msg1 = new Msg(text,Msg.TYPE_RECEIVED);
                msgList.add(msg1);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onRecvC2CReadReceipt(List<V2TIMMessageReceipt> receiptList) {
                super.onRecvC2CReadReceipt(receiptList);
            }

            @Override
            public void onRecvMessageRevoked(String msgID) {
                super.onRecvMessageRevoked(msgID);
            }
        });


    }

//    private void initMsgs() {
//        Msg msg1 = new Msg("Hello guy.", Msg.TYPE_RECEIVED);
//        msgList.add(msg1);
//      //Msg msg2 = new Msg("Hello. Who is that?", Msg.TYPE_SEND);
//        // msgList.add(msg2);
//        Msg msg3 = new Msg("This is Tom. Nice talking to you.", Msg.TYPE_RECEIVED);
//        msgList.add(msg3);
//    }
    }

